var app = getApp();
var request = app.request;
var util = require('../../../utils/util.js');

const date = new Date();
const years = [];
const months = [];
const days = [];
const hours = [];
const mins = [];

for (let i = 2024; i <= date.getFullYear(); i++) {
  years.push(i);
}

for (let i = 1; i <= 12; i++) {
  months.push(i.toString().length>1?i:('0'+i));
}

for (let i = 1; i <= 31; i++) {
  days.push(i.toString().length>1?i:('0'+i));
}

for (let i = 0; i <= 23; i++) {
  hours.push(i.toString().length>1?i:('0'+i));
}

for (let i = 0; i <= 59; i++) {
  mins.push(i.toString().length>1?i:('0'+i));
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showDialog: false,
    name: "",
    liaison: "",
    mobile: "",
    meal_label: "",
    reservation_time: "",
    address: "",
    years,
    year: date.getFullYear(),
    months,
    month: date.getMonth(),
    days,
    day: date.getDate() - 1,
    hours,
    hour: date.getHours(),
    mins,
    min: date.getMinutes(),
    value: [9999, date.getMonth(), date.getDate() - 1, date.getHours(), date.getMinutes()],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.initData();
  },
  setName(e) {
    this.setData({
      name: e.detail.value
    })
  },
  setLiaison(e) {
    this.setData({
      liaison: e.detail.value
    })
  },
  setMobile(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  setMealLabel(e) {
    this.setData({
      meal_label: e.detail.value
    })
  },
  closeTime() {
    this.setData({
      showDialog: false
    })
  },
  showTime() {
    this.setData({
      showDialog: true
    })
  },
  setAddress(e) {
    this.setData({
      address: e.detail.value
    })
  },
  bindChange(e) {
    const val = e.detail.value
    this.setData({
      reservation_time: this.data.years[val[0]]+'-'+this.data.months[val[1]]+'-'+this.data.days[val[2]]+' '+this.data.hours[val[3]]+':'+this.data.mins[val[4]],
      // year: this.data.years[val[0]],
      // month: this.data.months[val[1]],
      // day: this.data.days[val[2]],
    })
  },
  submit() {
    var that = this;
    if(!that.data.name || !that.data.liaison || !that.data.mobile || !that.data.meal_label || !that.data.reservation_time || !that.data.address) {
      app.showTextWarining("信息不全，请重新输入");
      return;
    }
    request.post('/api/index/addEnterprise', {
      data: {
        name: that.data.name,
        liaison: that.data.liaison,
        mobile: that.data.mobile,
        meal_label: that.data.meal_label,
        reservation_time: new Date(that.data.reservation_time).getTime()/1000,
        address: that.data.address
      },
      failRollback: false,
      successReload: true,
      success: function (res) {
        if (res.data.status == 1) {
          that.onLoad();
          app.showTextWarining('提交成功');
        } else {
          app.showTextWarining(res.data.msg);
        }
      }
    });
  },
  initData() {
    console.log(123)
    this.setData({
      name: "",
      liaison: "",
      mobile: "",
      meal_label: "",
      reservation_time: this.data.year+'-'+(this.data.month+1)+'-'+(this.data.day+1)+' '+this.data.hour+':'+this.data.min,
      address: "",
    })
  }
})